package com.dangz.dans.utils;

import com.alibaba.fastjson.JSON;
import com.dangz.dans.aes.WXBizMsgCrypt;
import org.apache.commons.codec.binary.Base64;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.util.HashMap;
import java.util.Map;

/**
 *  小程序解密encryptedData
 * @author dangz
 * @since 2018/4/2 15:05
 */
public abstract class EncryptedDataUtils {
    public static String decode(String encryptedData, String session_key, String iv){
        Map map = new HashMap();
        try {
            byte[] resultByte  = WXBizMsgCrypt.decrypt(Base64.decodeBase64(encryptedData),
                    Base64.decodeBase64(session_key),
                    Base64.decodeBase64(iv));
            if(null != resultByte && resultByte.length > 0){
                String userInfo = new String(resultByte, "UTF-8");
                map.put("status", "1");
                map.put("msg", "解密成功");
                map.put("userInfo", userInfo);
            }else{
                map.put("status", "0");
                map.put("msg", "解密失败");
            }
        }catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String decodeJSON = JSON.toJSONString(map);
        System.out.println(decodeJSON);
        return decodeJSON;
    }
}
